/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_SIP_TCP_LISTENER_H
#define MK_SIP_TCP_LISTENER_H
#include "MkSipListener.h"
#include "MkTcp/MkTcpServer.h"
#include "MkSipMessage.h"

class MkApi CMkSipTcpListener : public CMkSipListener, public CMkTcpServer
{
public:
    CMkSipTcpListener(CMkEventPool* pEventPool, CMkTimer* pTimer, CMkSipServer* pParent);
    virtual ~CMkSipTcpListener();

    Uint32 Start(const MkString& ListenHost, Uint16 ListenPort, const MkString& PublicHost, Uint16 PublicPort) override;
    Uint32 SendSipMessage(const CMkSipMessage& SipMessage, const SipMsgCompleteCallback& Cb = nullptr, BOOL bResend = FALSE) override;
    Uint32 OnSipRecved(const Uint8* pBuf, Uint32 nLen, const MkString& RemoteHost, Uint16 RemotePort, const MkString& LocalHost, Uint16 LocalPort, Uint32& nUsed);
protected:
private:
    CMkTcpSession* CreateNewSession(CMkEvent *pEvent, CMkTimer* pTimer) override;
    Uint32 OnSessionAccepted(CMkTcpSession *pSession) override;
    Uint32 OnSessionEventRemoved(CMkTcpSession* pSession) override;
    Uint32 SocketSendSipMessage(CMkSocket* pSocket, const CMkSipMessage& SipMessage, const SipMsgCompleteCallback& Cb = nullptr, BOOL bResend = FALSE);
private:
    CMkRwLock                               m_TcpRouterLock;
    MkMap<MkString, CMkSocket*>             m_MapTcpRouter;  //key remote ip+port
};


#endif